package com.tanjun.mallservice.web.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tanjun.mallservice.web.orm.entity.MallPluSku;
import com.tanjun.mallservice.web.orm.mapper.MallPluSkuMapper;
import com.tanjun.mallservice.web.service.MallPluSkuService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @author jjtan
 * @description 针对表【mall_plu_sku(商品规格)】的数据库操作Service实现
 * @createDate 2024-03-21 14:42:17
 */
@Service
public class MallPluSkuServiceImpl extends ServiceImpl<MallPluSkuMapper, MallPluSku>
        implements MallPluSkuService {

    /**
     * 获取指定商品的规格列表
     *
     * @param pluId 商品ID
     * @return
     */
    @Override
    @Cacheable(value = "getPluSkuList", key = "#pluId")
    public List<MallPluSku> getPluSkuList(Long pluId) {
        return this.list(QueryChain.of(MallPluSku.class)
                .eq(MallPluSku::getPluId, pluId)
        );
    }

    /**
     * 保存商品规格列表
     *
     * @param listSku
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = {"getPluSkuList", "getSkuById"}, allEntries = true)
    public void saveSkuList(List<MallPluSku> listSku) {
        for (MallPluSku sku : listSku) {
            if (sku.getSkuId() == null) {
                this.save(sku);
            } else {
                this.updateById(sku);
            }
        }
    }

    /**
     * 根据Id获取规格
     *
     * @param skuId
     */
    @Override
    @Cacheable(value = "getSkuById", key = "#skuId")
    public MallPluSku getSkuById(Long skuId) {
        return this.getById(skuId);
    }

    @Override
    public List<MallPluSku> listSkuByIds(Collection<Long> skuIds) {
        return this.listByIds(skuIds);
    }
}




